package md.lanjing.com.module_deal_center.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.base.XActivity;
import com.lxh.baselibray.constant.ARouterConfig;

import butterknife.BindView;
import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.ui.fragment.BuyOrderFragment;
import md.lanjing.com.module_deal_center.ui.fragment.ConsignOrderFragment;


/**
 * 订单
 */
public class OrderActivity extends XActivity {
    public static final String FROM ="from";
    @BindView(R2.id.rg)
    RadioGroup rg;
    private Fragment showFragment;

    @Override
    public void initData(Bundle savedInstanceState) {
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_consign_order) {
                fragmentManager(R.id.fragment_container, new ConsignOrderFragment(), "consignOrder");

            } else if (checkedId == R.id.rb_buy_order) {
                fragmentManager(R.id.fragment_container, new BuyOrderFragment(), "buyOrder");

            }
        });
        int position = getIntent().getIntExtra(FROM, 0);//0-寄售 1购买
        rg.getChildAt(position).performClick();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_order;
    }

    private void fragmentManager(int resid, Fragment fragment, String tag) {
        FragmentManager fragmentManager =getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //隐藏正在显示的Fragment对象
        if (showFragment != null) {
            fragmentTransaction.hide(showFragment);
        }

        Fragment mFragment = fragmentManager.findFragmentByTag(tag);
        if (mFragment != null) {
            fragmentTransaction.show(mFragment);
        } else {
            mFragment = fragment;
            fragmentTransaction.add(resid, mFragment, tag);
        }
        showFragment = mFragment;
        fragmentTransaction.commit();
    }

}
